﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Threading;

namespace PhoneApp6
{
    public partial class Page1 : PhoneApplicationPage
    {
        
        string flstr;
        private IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
        
        public Page1()
        {
            InitializeComponent();
        }
        public void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            cl cl = new cl();
           
            cl.seach();
            cl.OnLoadDataCompleted += OnLoadDataCompleted;
             
            
        }


        private void OnLoadDataCompleted(System.Collections.ObjectModel.ObservableCollection<dls> dtc)
        {
            this.lst.DataContext = dtc;
        }
       
       
      

       

        private void extn(object sender, RoutedEventArgs e)
        {
            Button mtbtm = (Button)sender;
            StackPanel mypn = (StackPanel)((StackPanel)mtbtm.Parent).Parent;
            StackPanel mysk = (StackPanel)mypn.FindName("skp");
            if (mysk.Visibility == Visibility.Visible)
            {
                mysk.Visibility = Visibility.Collapsed;
            }
            else
            { 
                mysk.Visibility = Visibility.Visible;
            }
        }

       
        
      
        private void brow(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Page2.xaml",UriKind.Relative));
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
        
            
              
            if ( NavigationContext.QueryString["flstr"]!= "" )
            {
                cl cl = new cl();
                this.flstr = NavigationContext.QueryString["flstr"];
                dls newdls = new dls();
                {
                    newdls.no = 0;
                    newdls.dln = cnvr(flstr);
                    newdls.dlu = flstr;
                    newdls.dlx = "nolixian";
                }
                System.Collections.ObjectModel.ObservableCollection<dls> localdls = cl.frmlocal1();
                localdls.Add(newdls);
                cl.savetolocal(localdls);
                this.lst.DataContext = localdls.OrderBy(c => c.no).ToList();



            }

        }
       

        public string cnvr(string orgflu)
        {
            string cnvrout = orgflu.Substring(orgflu.LastIndexOf("/") + 1);
            return cnvrout;
        }



        private void Image_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            Image favimg = (Image)sender;
            string imgtag = favimg.Tag.ToString();
           App.mt = new Mutex(true,"mymt");
            cl cl = new cl();
            Thread t1 = new Thread(() => cl.addtsk(imgtag));
            t1.Start();
            cl.OnLoadDataCompleted += OnLoadDataCompleted;
            App.mt.ReleaseMutex();
            //Thread.Sleep(1000);
           

           // MessageBox.Show("添加下载文件成功");
            if (MessageBox.Show("确定刷新服务器上的列表？", "添加下载文件成功", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {

               
               
              //  cl1.OnLoadDataCompleted1 += OnLoadDataCompleted;
                
               // cl1.frmlocal(); 
                Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                           
                           // cl1.OnLoadDataCompleted += OnLoadDataCompleted;
                        }));
               
            }
            else
            {
                return;
            }
        }

       

        private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/SettingPage.xaml", UriKind.Relative));
        }

        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            cl cl = new cl();
            cl.OnLoadDataCompleted1 += OnLoadDataCompleted;
            cl.frmlocal();
        }

        private void ApplicationBarIconButton_Click_2(object sender, EventArgs e)
        {
            cl cl = new cl();
            
            cl.frmnet(false);
            cl.OnLoadDataCompleted += OnLoadDataCompleted;
          
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            cl cl = new cl();
            App.mt = new Mutex(true, "mymt");
            System.Collections.ObjectModel.ObservableCollection<dls> localdls = cl.frmlocal1();
            foreach (dls mys in localdls.Where(h => h.no == 0))
            {
              new Thread(() => cl.addtsk(mys.dlu.ToString())).Start();
                
               // cl.addtsk(mys.dlu.ToString());
                cl.OnLoadDataCompleted += OnLoadDataCompleted;
                Thread.Sleep(20000);
                App.mt.ReleaseMutex();
            }
            MessageBox.Show("添加所有新的下载文件成功");
        }

        
    }
}